﻿@using PagedList.Mvc;
@using PagedList;
@using TestOnlineProject.Models.Entities;
@using TestOnlineProject.Common;
<script>
    $(document).ready(function () {
        // Select all.
        $("#select_all").click(function () {
            $(".is_selected").attr('checked', true);
        });

        // Unselect all.
        $("#unselect_all").click(function () {
            $(".is_selected").attr('checked', false);
        });

        $("#deactivate").click(function () {
            var con = confirm("Are you sure you want to deactivate this participant(s)?");
            if (con == true) {
                var val = "";
                $(':checkbox:checked').each(function (i) {
                    val += $(this).val() + "-";
                });
                if (val == "") {
                    alert("You must select at least one participant.")
                }
                else {
                    // Deactivate.
                    $.ajax({
                        url: "@Url.Content("~/Participant/Deactivate")?userIds=" + val + "&subjectId=" + @ViewBag.SubjectId,
                        type: 'GET',
                        success: function (data) {
                            if (data != null) {
                                alert(data);
                                // Reload.
                                $.ajax({
                                    url: "@Url.Content("~/Participant/Index")?page=@ViewBag.Page&subjectId=@ViewBag.SubjectId&keyWord=@ViewBag.KeyWord&column=@ViewBag.Column&sort=@ViewBag.Sort",
                                    type: 'GET',
                                    success: function (data) {
                                        $("#unobtrusive").html(data);
                                    }
                                });
                            }
                        }
                    });
                }
            }
        });

        $("#activate").click(function () {
            var con = confirm("Are you sure you want to activate this participant(s)?");
            if (con == true) {
                var val = "";
                $(':checkbox:checked').each(function (i) {
                    val += $(this).val() + "-";
                });
                if (val == "") {
                    alert("You must select at least one participant.")
                }
                else {
                    // Activate.
                    $.ajax({
                        url: "@Url.Content("~/Participant/Activate")?userIds=" + val + "&subjectId=" + @ViewBag.SubjectId,
                        type: 'GET',
                        success: function (data) {
                            if (data != null) {
                                alert(data);
                                // Reload.
                                $.ajax({
                                    url: "@Url.Content("~/Participant/Index")?page=@ViewBag.Page&subjectId=@ViewBag.SubjectId&keyWord=@ViewBag.KeyWord&column=@ViewBag.Column&sort=@ViewBag.Sort",
                                    type: 'GET',
                                    success: function (data) {
                                        $("#unobtrusive").html(data);
                                    }
                                });
                            }
                        }
                    });
                }
            }
        });

        $("#delete").click(function () {
            var con = confirm("Are you sure you want to delete this participant(s)?");
            if (con == true) {
                var val = "";
                $(':checkbox:checked').each(function (i) {
                    val += $(this).val() + "-";
                });
                if (val == "") {
                    alert("You must select at least one participant!")
                }
                else {
                    // Activate.
                    $.ajax({
                        url: "@Url.Content("~/Participant/Delete")?userIds=" + val + "&subjectId=" + @ViewBag.SubjectId,
                        type: 'GET',
                        success: function (data) {
                            if (data != null) {
                                alert(data);
                                // Reload.
                                $.ajax({
                                    url: "@Url.Content("~/Participant/Index")?page=@ViewBag.Page&subjectId=@ViewBag.SubjectId&keyWord=@ViewBag.KeyWord&column=@ViewBag.Column&sort=@ViewBag.Sort",
                                    type: 'GET',
                                    success: function (data) {
                                        $("#unobtrusive").html(data);
                                    }
                                });
                            }
                        }
                    });
                }
            }
        });

    });
</script>
<div id="list">
    <div>
        <table class="table table-hover">
            <tr>
                <th style="vertical-align: middle; width: 50px">
                    @Ajax.ActionLink("No.", "Index", new { subjectId = @ViewBag.SubjectId, column = "No", sort = (@ViewBag.Sort == "Asc" && @ViewBag.Column == "No") ? "Desc" : "Asc", keyWord = @ViewBag.KeyWord }, new AjaxOptions { UpdateTargetId = "list", InsertionMode = InsertionMode.Replace })
                </th>
                <th style="vertical-align: middle">
                    @Ajax.ActionLink("User", "Index", new { subjectId = @ViewBag.SubjectId, column = "FullName", sort = (@ViewBag.Sort == "Asc" && @ViewBag.Column == "FullName") ? "Desc" : "Asc", keyWord = @ViewBag.KeyWord }, new AjaxOptions { UpdateTargetId = "list", InsertionMode = InsertionMode.Replace })
                </th>
                @{
                    if (HttpContext.Current.User.IsInRole("1"))
                    {
                    <th style="width: 35px">
                        <div class="btn-group">
                            <a class="btn dropdown-toggle" data-toggle="dropdown" href="#"><span class="caret"></span>
                            </a>
                            <ul class="dropdown-menu pull-right">
                                <li>
                                    <button class="btn btn-link" id="select_all">
                                        Select All</button></li>
                                <li>
                                    <button class="btn btn-link" id="unselect_all">
                                        Unselect All</button></li>
                                <li>
                                    <button class="btn btn-link" id="deactivate">
                                        Deactivate</button></li>
                                <li>
                                    <button class="btn btn-link" id="activate">
                                        Activate</button></li>
                                <li>
                                    <button class="btn btn-link" id="delete">
                                        Delete</button></li>
                            </ul>
                        </div>
                    </th>
                    }
                }
            </tr>
            @{
                var row = 0;
                foreach (var item in (ViewBag.onePageOfParticipants as IEnumerable<Participant>))
                {
                    row++;
                    <tr id="row_@row">
                        <td>
                            @{
                                var noList = ViewBag.NoList as List<NoModel>;
                                var no = noList.Where(n => n.Id == item.UserId).FirstOrDefault().No;
                                <span style="word-wrap: break-word">@no</span>
                            }
                        </td>
                        <td>
                            @Html.ActionLink(item.User.FullName, "Details", "Participant", new {userId = item.UserId, subjectId = item.SubjectId}, null)
                        </td>
                        @{
                            if (HttpContext.Current.User.IsInRole("1"))
                            {
                                <td>
                                    <input type="checkbox" value="@item.UserId" class="is_selected"/>
                                </td>
                            }
                            if (!item.IsActivated)
                            {
                                <script>$("#row_@row").addClass("deactivated_item"); </script>
                            }
                        }
                    </tr>
                }
            }
        </table>
    </div>
    @Html.PagedListPager((IPagedList)ViewBag.onePageOfParticipants, page => Url.Action("Index", new { subjectId = @ViewBag.SubjectId, page, sort = @ViewBag.Sort, column = @ViewBag.Column, keyWord = @ViewBag.KeyWord }), PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(new AjaxOptions() { HttpMethod = "GET", UpdateTargetId = "unobtrusive", OnComplete = "" }))
</div>
